/*!
\copyright  Copyright (c) 2024 - 2025 Qualcomm Technologies International, Ltd.\n
            All Rights Reserved.\n
            Qualcomm Technologies International, Ltd. Confidential and Proprietary.
\file       
\defgroup   fast_pair_domain    Fast Pair
\ingroup    bt_domain
\brief 
*/

#include <logging.h>
#include <fast_pair.h>
#include "fast_pair_ui_messages.h"
#include "fast_pair_msg_stream_dev_action.h"
#include "task_list.h"
#include "domain_message.h"
#include "ui_indicator_prompts.h"
#include "ui.h"
#include "ui_inputs.h"

#ifdef INCLUDE_FAST_PAIR
#define FAST_PAIR_UI_CLIENT_TASK_LIST_INIT_CAPACITY 1
TASK_LIST_WITH_INITIAL_CAPACITY(FAST_PAIR_UI_CLIENT_TASK_LIST_INIT_CAPACITY) client_list;
#define FastPairUiMessages_GetClientList()      (task_list_flexible_t *)&client_list
bool is_initialised = FALSE;
#else
#define FastPairUiMessages_GetClientList()      NULL
#endif

static inline unsigned fastPairUiMessages_GetMessageForRingVolumeLevel(ringtone_volume_t ring_volume)
{
    unsigned locate_event = FAST_PAIR_LOCATE_DEVICE_MAX_VOLUME;
    switch(ring_volume)
    {
        case ring_vol_low:
            locate_event = FAST_PAIR_LOCATE_DEVICE_LOW_VOLUME;
            break;
        case ring_vol_medium:
            locate_event = FAST_PAIR_LOCATE_DEVICE_MEDIUM_VOLUME;
            break;
        case ring_vol_high:
            locate_event = FAST_PAIR_LOCATE_DEVICE_HIGH_VOLUME;
            break;
        case ring_vol_max:
        default:
            locate_event = FAST_PAIR_LOCATE_DEVICE_MAX_VOLUME;
            break;
    }
    return locate_event;
}

void FastPairUiMessages_StartRingtone(ringtone_volume_t volume)
{
    DEBUG_LOG("FastPairUiMessages_StartRingtone volume=%d", volume);
    TaskList_MessageSendId(TaskList_GetFlexibleBaseTaskList(FastPairUiMessages_GetClientList()), fastPairUiMessages_GetMessageForRingVolumeLevel(volume));
}

void FastPairUiMessages_StopRingtone(void)
{
    DEBUG_LOG("FastPairUiMessages_StopRingtone");
    UiPrompts_ForceStopPrompt();
}

static unsigned fastPairUiMessages_GetCurrentUiContext(void)
{
    return (fpRingDevice_GetTaskData()->is_device_ring ? context_fast_pair_device_ring_active : context_fast_pair_idle);
}

void FastPairUiMessages_InformContextUpdate(void)
{
    Ui_InformContextChange(ui_provider_fast_pair, fastPairUiMessages_GetCurrentUiContext());
}

void FastPairUiMessages_Init(void)
{
    Ui_RegisterUiProvider(ui_provider_fast_pair, fastPairUiMessages_GetCurrentUiContext);
}

#ifdef INCLUDE_FAST_PAIR

static void FastPairUiMessages_RegisterMessageGroup(Task task, message_group_t group)
{
    if(!is_initialised)
    {
        TaskList_InitialiseWithCapacity(FastPairUiMessages_GetClientList(), FAST_PAIR_UI_CLIENT_TASK_LIST_INIT_CAPACITY);
        is_initialised = TRUE;
    }
    DEBUG_LOG("FastPairUiMessages_RegisterMessageGroup");
    PanicFalse(group == FAST_PAIR_UI_MESSAGE_GROUP);
    TaskList_AddTask(TaskList_GetFlexibleBaseTaskList(FastPairUiMessages_GetClientList()), task);
}

MESSAGE_BROKER_GROUP_REGISTRATION_MAKE(FAST_PAIR_UI, FastPairUiMessages_RegisterMessageGroup, NULL);

#endif